package 关于重写Object类的equals方法相关问题;

import java.util.Objects;

public class Student_3 {
    private int id;
    private String name;
    private int age;
    private String score;

    public Student_3() {
    }

    public Student_3(int id, String name, int age, String score) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.score = score;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getScore() {
        return score;
    }

    public void setScore(String score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "student_3{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", score='" + score + '\'' +
                '}';
    }

    /* -------------------------------------------------------------------------------------*/
    /**
     * 不接受子类作为参数的equals方法,没有非空字段
     * @param o
     * @return
     */

    public boolean equals1(Object o) {
        //如果两个对象地址相同，则对象肯定相等
        if (this == o) return true;
        //若比较的对象o为空，或者比较的对象的字节码文件不相同，则两个对象肯定不相等
        if (o == null || getClass() != o.getClass()) return false;
        //若两个对象的属性值都相同，则表示两个对象相等
        Student_3 student_3 = (Student_3) o;
        return id == student_3.id && age == student_3.age && Objects.equals(name, student_3.name) && Objects.equals(score, student_3.score);
    }


    /**
     * 接受子类作为参数的equals方法,没有非空字段
     * @param o
     * @return
     */

    public boolean equals2(Object o) {
        if (this == o) return true;
        //若比较对象o不为student_3类的实例对象，则两个对象不相等
        if (!(o instanceof Student_3)) return false;
        Student_3 student_3 = (Student_3) o;
        return id == student_3.id && age == student_3.age && Objects.equals(name, student_3.name) && Objects.equals(score, student_3.score);
    }

    /**
     * 接受子类作为参数的equals方法,没有非空字段
     * 且在代码生成过程中使用getters方法
     * @param o
     * @return
     */

    public boolean equals3(Object o) {
        if (this == o) return true;
        if (!(o instanceof Student_3)) return false;
        Student_3 student_3 = (Student_3) o;
        return getId() == student_3.getId() && getAge() == student_3.getAge() && Objects.equals(getName(), student_3.getName()) && Objects.equals(getScore(), student_3.getScore());
    }
}
